{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "b6331617",
   "metadata": {},
   "source": [
    "# 本周知识点"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b2d82c2b",
   "metadata": {},
   "source": [
    "## list，字典"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4d4b434d",
   "metadata": {},
   "source": [
    "### vowels.py"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "ba099466",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "i\n",
      "i\n",
      "a\n"
     ]
    }
   ],
   "source": [
    "vowels = ['a','e','i','o','u']\n",
    "word = \"Milliways\"\n",
    "# 检查word中出现的元音，并打印出来 怎么做？\n",
    "#for letter in word: #第一个元素letter 请不要和已有变量重名\n",
    "                    #第一个元素一般不定义名字，常用item(元素)来命名\n",
    "                    #                        简写i来命名\n",
    "                    #第二个元素(重要)，意味着将遍历的内容\n",
    "                   #遍历的内容一般是 序列数据 str list\n",
    "                \n",
    "for letter in word:\n",
    "#print(letter)\n",
    "    if letter in vowels:\n",
    "        print(letter)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8acbcc07",
   "metadata": {},
   "source": [
    "### vowels2.py"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "ad539a4e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['i', 'a']"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#使用append()新建列表\n",
    "found = []\n",
    "for letter in word:\n",
    "     #print(letter)\n",
    "    if letter in vowels:    # letter 在元音字母表中\n",
    "        if letter not in found: #letter不在found表\n",
    "            found.append(letter)\n",
    "found"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c8e21d44",
   "metadata": {},
   "source": [
    "## 字典"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "039e1302",
   "metadata": {},
   "source": [
    "### 1. 字典的初始 （{key:value}）"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a8fe9a0f",
   "metadata": {},
   "source": [
    " 1. [字典的文档](https://docs.python.org/3/tutorial/datastructures.html#dictionaries) \n",
    " 2. 字典的重要属性：\n",
    " 1. 结构：key:value（键值对）\n",
    " 2. 字典是无序的，我们通过键(key)去查找值(value)，就好像list用索引(index)查找值(value)一样，只不过字典的“索引”被定义了名字\n",
    " 3. 字典对key的定义有一定的要求：key是由\"不可变\"的数据结构才可以定义，list是不能作为key来定义的。\n",
    " 4. 字典的key是唯一的，字典的值value也可以不唯一。（list的值是唯一的么？）\n",
    " 3. 字典的创建"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "acdf44e7",
   "metadata": {},
   "outputs": [],
   "source": [
    "users = [\n",
    "    [\"siwen\",23,\"IT\",20000],\n",
    "    [\"liumang\",26,\"IT\",30000]\n",
    "\n",
    "]\n",
    "# 试对比:\n",
    "user_dict = {\n",
    "    \"siwen\":[23,\"IT\",20000],\n",
    "    \"liumang\":[26,\"IT\",30000]\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "b9a2aecb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['liumang', 26, 'IT', 30000]"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 查询liumang的信息：\n",
    "users[1]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3f6f4d78",
   "metadata": {},
   "source": [
    "### 2. 字典的创建(常见的三种创建方式)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "26ec4a7b",
   "metadata": {},
   "source": [
    "1. info = {key1:value1 , key2:value2 , ...}直接按照字典格式创建\n",
    "2. info_02 = dict(key1=value1 , key2=value2 , ... ) 使用dict()进行字典创建\n",
    "3. 运用字典增加的方式新建字典:\n",
    "``` \n",
    "info_03 = {} info_03[key1] = value1 ,info_03[key2] = value2 ... \n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "6d06ec95",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'stu10001': 'siwen', 'stu10002': 'liumang', 'stu10003': 'jianglaoban', 'stu10004': 'jianglaoban'}\n",
      "<class 'dict'>\n"
     ]
    }
   ],
   "source": [
    "info = {\n",
    "    \"stu10001\":\"siwen\",\n",
    "    \"stu10002\":\"liumang\",\n",
    "    \"stu10003\":\"jianglaoban\",\n",
    "    \"stu10004\":\"jianglaoban\",\n",
    "}\n",
    "# \n",
    "print(info)\n",
    "print(type(info))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "9d9d1674",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'stu10001': 'siwen', 'stu10002': 'liumang', 'stu10003': 'jianglaoban'}\n",
      "<class 'dict'>\n"
     ]
    }
   ],
   "source": [
    "info_02 = dict(\n",
    "    stu10001 = \"siwen\",\n",
    "    stu10002 = \"liumang\",\n",
    "    stu10003 = \"jianglaoban\",\n",
    ")\n",
    "print(info_02)\n",
    "print(type(info_02))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "b61eedab",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'stu10001': 'siwen', 'stu10002': 'liumang', 'stu10003': 'jianglaoban'}\n",
      "<class 'dict'>\n"
     ]
    }
   ],
   "source": [
    "info_03 ={} # 新建字典\n",
    "info_03[\"stu10001\"] = \"siwen\"\n",
    "info_03[\"stu10002\"] = \"liumang\"\n",
    "info_03[\"stu10003\"] = \"jianglaoban\"\n",
    "print(info_03)\n",
    "print(type(info_03))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b4c4c75e",
   "metadata": {},
   "source": [
    "### 字典的增、删、改、查"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "201b4253",
   "metadata": {},
   "source": [
    "1. 字典的增加(和字典的第三种创建方式一致)：\n",
    " * info_03[\"stu10004\"] = \"zhichao\"\n",
    " 2. 字典的查询\n",
    " * info_03[key_name]\n",
    " * 注意： 正确的查询方式，应该先查询key是否存在字典当中，如下：\n",
    " 1. key_name in info_03  ## 查看是否存在,如果存在，返回结果为 True ，不存在为 False\n",
    " 2. info_03[key_name]\n",
    " 3. 字典的修改\n",
    " 1. 查询key是否存在 \n",
    " 2. 修改key的值\n",
    " * 例如： 修改info_03中stu10003的值为Alex, if \"stu10003\" in info_03 : info_03 [\"stu10003\"] = \"Alex\"\n",
    " 4. 字典的删除\n",
    " 1.pop(key)\n",
    " 2.popitem()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "6112aea6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'stu10001': 'siwen', 'stu10002': 'liumang', 'stu10003': 'jianglaoban', 'stu10004': 'liliumang'}\n"
     ]
    }
   ],
   "source": [
    "info_03 [\"stu10004\"] = \"liliumang\"\n",
    "print(info_03)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "26f5d6a4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'jianglaoban'"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 用key查到value的数据\n",
    "info_03[\"stu10003\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "4765869a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "查询方法不存在\n"
     ]
    }
   ],
   "source": [
    "# 正确的查询字典的方法\n",
    "print(\"stu10003\" in info_03)\n",
    "if \"stu10006\" in info_03:\n",
    "    print(info_03[\"stu10006\"])\n",
    "else:\n",
    "    print(\"查询方法不存在\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "d66c6076",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'stu10001': 'siwen', 'stu10002': 'liumang', 'stu10003': 'jianglaoban', 'stu10004': 'liliumang', 'stu_03': 'Alex'}\n"
     ]
    }
   ],
   "source": [
    "# 修改:也要先查询key\n",
    "if \"stu10003\" in info_03:\n",
    "    info_03 [\"stu_03\"] = \"Alex\"\n",
    "print(info_03)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9375045f",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
